package com.kern.service;

import com.kern.bean.DsConnection;
import com.kern.entity.XProduct;
import com.kern.util.JDBCUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by caoshi at 9:55 2021/7/29
 */
@Service
public class TestJDBCService {


    @Autowired
    private DsConnection conn;

    public void testQuery() {
        Connection connection = null;
        PreparedStatement stat = null;
        try {
            connection = JDBCUtil.getConnection(conn);
            String sql = "select * from lj_product";
            stat = connection.prepareStatement(sql);
            ResultSet resultSet = stat.executeQuery();

            if (resultSet != null) {
                List<XProduct> productList = new ArrayList<>();
                while (resultSet.next()) {
                    XProduct p = new XProduct();
                    p.setId(resultSet.getString("lj_product_id"));
                    p.setName(resultSet.getString("lj_product_name"));
                    p.setSubCategory(resultSet.getString("lj_product_subcategory"));
                    p.setCategory(resultSet.getString("lj_product_category"));
                    productList.add(p);
                }
                System.out.println(productList);
            }

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, stat);
        }


    }




}
